from Utils.sql_parser_test import SqlParserTest
import pymysql.cursors
import configparser

config = configparser.ConfigParser()
config.read('config.ini', encoding="utf-8-sig")
# 连接数据库
connection = pymysql.connect(host=config['database']['host'],
                             user=config['database']['user'],
                             password=config['database']['password'],
                             db=config['database']['db'],
                             charset=config['database']['charset'],
                             cursorclass=pymysql.cursors.DictCursor)

try:
    with connection.cursor() as cursor:
        for i in range(int(config['sql']['loop'])):
        # Create a new record
            # name_sex = GenName().generate_name_sex()
            # email = GenEmail().generate_email(name_sex["name"])
            # grade = GenInt().generate_int(60, 100)
            # sql = f'INSERT INTO `user` (`username`, `email`, `grade`, `sex`) \
            #         VALUES ("{name_sex["name"]}", "{email}", {grade}, "{name_sex["sex"]}")'
            sql = config['sql']['sql']
            print(sql)
            # values = SqlParser(sql).replace_sql()
            values = SqlParserTest(sql).replace_params()
            print("values: ", values)
            values_to_str = ",".join(values)
            sql = sql.split("values")[0] + "values (" + values_to_str + ")"
            print("sql: ", sql)
            cursor.execute(sql)
            
    connection.commit()

finally:
    connection.close()